package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /*
     * 套餐分页查询
     * */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {

        //调用分页插件
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //返回Page封装的对象，因为page里面有getTotal，getResult，可以调用page里面的对象方法
        Page<SetmealVO> p= setmealMapper.page(dto);

        return new PageResult(p.getTotal(),p.getResult());





    }
    /*
     * 修改套餐
     * */
    @Override
    @Transactional
    public void update(SetmealDTO dto) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);

        setmealMapper.update(setmeal);

        Long setmealId=setmeal.getId();

        //修改：先删后增
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishList=dto.getSetmealDishes();

        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        setmealDishMapper.insertSetmealDish(setmealDishList);






    }
    /*
     * 新增套餐
     * */
    @Override
    @Transactional
    public void insert(SetmealDTO dto) {
        //插入setmeal表数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.insertSetmeal(setmeal);
        //获取生成的套餐id

        //获取setmealDishes数据，在另一张表中setmealDish


        List<SetmealDish> setmealDishList=dto.getSetmealDishes();
        //如果！= null 并且>0
        if(setmealDishList !=null && setmealDishList.size()>0){
            setmealDishList.forEach(setmealDish -> {
                //进行逻辑外键的绑定
                setmealDish.setSetmealId(setmeal.getId());
            });
            //遍历插入插入SetmealDish表
            setmealDishMapper.insertSetmealDish(setmealDishList);
        }


    }
    /*
     * 根据id查询套餐
     * */
    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal= setmealMapper.getById(id);
        List<SetmealDish> setmealDishList= setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }
    /*
     * 批量删除套餐
     * */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id ->
                {
                    Setmeal setmeal=setmealMapper.getById(id);
                 if(setmeal.getStatus()== StatusConstant.ENABLE){
                     //起售中的套餐不能删除
                     throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                 }

                }

                );
        ids.forEach( setmealId ->{
            setmealMapper.deleteById(setmealId);

            setmealDishMapper.deleteBySetmealId(setmealId);


        });



    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status == StatusConstant.ENABLE){
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);


    }
}
